import { _decorator, BoxCollider, Component, ITriggerEvent, Node } from 'cc';
import { GameManager } from './GameManager';
import { Constand } from './Constant';
import { PoolMananger } from './PoolMananger';
const { ccclass, property } = _decorator;

@ccclass('Item')
export class Item extends Component {
    private speed = 0.3;
    private speedX = 0.3;
    @property
    public type = Constand.BulletType.M
    onEnable() {
        const collider = this.getComponent(BoxCollider);
        collider.on('onTriggerEnter',this._onTriggerEnter, this);
    }

    onDisable(): void {
        const collider = this.getComponent(BoxCollider);
        collider.off('onTriggerEnter',this._onTriggerEnter, this);
    }

    private _onTriggerEnter(event: ITriggerEvent) {
        if(this.type == Constand.BulletType.H){
            GameManager.getInstance().changeBullet(Constand.BulletType.H);
        }else if(this.type == Constand.BulletType.S){
            GameManager.getInstance().changeBullet(Constand.BulletType.S);
        }else{
            GameManager.getInstance().changeBullet(Constand.BulletType.M);
        }
        PoolMananger.getInstance().putNode(this.node);
    }

    update(deltaTime: number) {
        const pos = this.node.position;
        if(pos.x >= 15){
            this.speedX = this.speed;
        }else if(pos.x <= -15){
            this.speedX = -this.speed;
        }
        this.node.setPosition(pos.x + this.speedX * deltaTime, pos.y, pos.z - this.speed * deltaTime);
        if(pos.z > 50){
            PoolMananger.getInstance().putNode(this.node);
        }
    }

    public setSpeed(value: number) {
        this.speed = value;
        this.speedX = value;
    }
}


